# 利用tailhead软件包跟踪文件变化
# 解析log文件，上传到thingsboard

import tailhead
# 字符串解析
import parse
# 时间包
import datetime
import os
import time
import regex as re

# 调用客户端
from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient

# 用于自动生成命令行
import fire

# 输出log，便于调试
import logging

def main(host='test2.bj-jiashi.com',
        port=10006,
        token='AURO', 
        delay_time=0.5, 
        target_log_file='sub3_AURO_simulator.py.test.log',
        log_level = 'INFO'):
    # 初始化
    logging.basicConfig(level = getattr(logging,log_level.upper(),logging.INFO)) # 默认info
    file_base_name = os.path.basename(__file__)
    logger = logging.getLogger(file_base_name)

    # 分隔符
    sep = ', '
    # 列名
    ## 33个
    col_names = 'dd,MM,yyyy,hh,mm,ss'.split(',')

    for angle in 'total,0,10,30,50,70,90'.split(','):
        for wavelength in [635,525,450]:
            col_names.append(f'sc_{wavelength}_{angle}')

    col_names += 'sample_air_temp,chassis_temp,RH,pressure,status1,status2'.split(',')
    # 默认值，为保持完整性
    defaults_str = '27/11/2018 08:13:30, 30.135, 31.907, 33.610, 27.750, 33.919, 31.836, 18.111, 21.656, 21.258, 15.053, 14.174, 14.797, 12.460, 9.788, 10.946, 9.387, 9.268, 9.021, 7.812, 7.815, 9.404, 4.078, 0.633, 6.951, 995.110, 00, 07'
    
    data_write_formats = '02d,02d,04d,02d,02d,02d'.split(',')+['.3f']*25+['2d']*2
    data_read_formats = [format[-1] for format in data_write_formats]

    # data types
    data_types = dict(zip(col_names, data_read_formats))

    # selected cols
    selected_cols = [col.strip() for col in 'sc_635_total,sc_525_total,sc_450_total'.split(',') ]

    # 生成格式化字符串
    datetime_formatter = '{dd:02d}/{MM:02d}/{yyyy:04d} {hh:02d}:{mm:02d}:{ss:02d}'
    line_write_formatter=datetime_formatter +sep + sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names[6:],data_write_formats[6:])])
    logger.info(line_write_formatter)

    datetime_formatter = '{dd:d}/{MM:d}/{yyyy:d} {hh:d}:{mm:d}:{ss:d}'
    line_read_formatter=datetime_formatter +sep + sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names[6:],data_read_formats[6:])])
    logger.info(line_read_formatter)

    # 简单测试
    parse_result = parse.parse(line_read_formatter,defaults_str)
    logger.info(parse_result)
    if parse_result is None:
        raise RuntimeError('Something go wrong!')

    # 默认值
    data_dict_defaults = parse_result.named
    
    # 建立设备连接
    TBD = TBDeviceMqttClient(host, port, token)
    TBD.connect()
    logger.info('mqtt server connected!')

    for line in tailhead.follow_path(target_log_file,encoding='utf-8'):
        if line is not None:
            logger.debug(f"readline: {line}")
            # 解析
            parse_result = parse.parse(line_read_formatter, line)
            if parse_result is None:
                logger.debug('not a valid data line')
                continue 
            data_line_dict = parse_result.named

            data_dict = {'ts':-1,'values':{}}
            # 时间解析
            # 假设是UTC时间，如果是北京时间需要调整
            time_datetime = datetime.datetime(  year=data_line_dict['yyyy'],
                                month=data_line_dict['MM'],
                                day=data_line_dict['dd'],
                                hour=data_line_dict['hh'],
                                minute=data_line_dict['mm'],
                                second=data_line_dict['ss'])

            data_dict['ts'] = int(time_datetime.timestamp()*1000) # 转换为ms数

            # 数据抽取
            data_dict['values'] = {col:data_line_dict.get(col,-99999) for col in selected_cols}

            logger.info(f'data_dict={data_dict}')

            # 上传
            TBD.send_telemetry(data_dict)
            time.sleep(delay_time)
        else:
            logger.info('sleep.')
            time.sleep(delay_time)

if __name__ == "__main__":
    fire.Fire(main)
    # e.g
    # python demo_tail_log_upload.py --DELAY 0.5 --LOG_FILE your_file.log